package com.cfr.utils;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateUtil {
	private static Logger logger = Logger.getLogger(DateUtil.class);

	public static String parseDateByAnyFormat(Date date, String format) {
		String returnValue = "";
		try {
			SimpleDateFormat formatter1 = new SimpleDateFormat(format);
			returnValue = formatter1.format(date);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return returnValue;
	}

	public static String parseDate(Date date) {
		String returnValue = "";
		try {
			SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
			returnValue = formatter1.format(date);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return returnValue;
	}

	public static String getYear(Date date) {
		DateFormat f_year = new SimpleDateFormat("yyyy");
		return f_year.format(date).toString();
	}

	public static String getMonth(Date date) {
		DateFormat f_month = new SimpleDateFormat("MM");
		return f_month.format(date).toString();
	}

	public static String getDay(Date date) {
		DateFormat f_day = new SimpleDateFormat("dd");
		return f_day.format(date).toString();
	}

	public static String getWeek(Date date) {
		DateFormat f_week = new SimpleDateFormat("EEEEEEE");
		return f_week.format(date).toString();
	}

	public static String getTime(Date date) {
		DateFormat f_time = new SimpleDateFormat("HH/mm/ss");
		return f_time.format(date).toString();
	}

	public static String getHour(Date date) {
		DateFormat f_time = new SimpleDateFormat("HH");
		return f_time.format(date).toString();
	}

	public static String getAMorPM(String date) {
		int index = date.indexOf(":") - 2;
		int hh = Integer.valueOf(date.substring(index, index + 2));
		if (hh >= 12) {
			return "PM";
		} else if (hh >= 0) {
			return "AM";
		} else {
			return "";
		}
	}

	public static String formatData(String date) {
		int index = date.indexOf(":") - 2;
		String formatDate = date.substring(0, index) + " "
				+ date.substring(index, date.length());
		if (date.indexOf(" ") > -1
				&& date.substring(index - 1, index).equals(" ")) {
			return date.trim();
		} else {
			return formatDate.trim();
		}
	}

}
